define([
		'app/service/LectureService',
		'app/utils/UploadPptUtils',
		'app/service/QiniuService',
		'plupload'
        ], function (
      	LectureService,
      	UploadPptUtils,
      	QiniuService
    ) {

	var opt = {};
	var roomuuid_current = "";
	var page_count = 0;
	var key = "";

	var flag = true;
	var flag2 = true;

	function progressFunc(up, file) {
       
       var now_value = parseInt(file.size * file.percent /100);
       $.promptProgressUpdate(file.percent + "%", plupload.formatSize(file.speed).toUpperCase(), plupload.formatSize(now_value).toUpperCase());

    }

	function beforeuploadFunc(up,file){

		$.closeModal();

		$.promptProgressSetup("PPT上传中<img style=\"margin-left:5px;  width: 18px;\"  src=\"img/loading.gif\">",plupload.formatSize(file.size).toUpperCase());
	}

	function uploadedFunc(info){

		$.closeModal();
		
		$.promptConvertProgressSetup("文档转换进行中<img style=\"margin-left:5px;  width: 18px;\"  src=\"img/loading.gif\">", "");

		// $.hidePreloader();
		// Object {hash: "FtRJBAplyOckAHWWLts855RvdOy7", key: "o_1aonhfltv186g1n9gp7k1se01qf7f.jpg"}

		// $.processing("文档转换中...", 1, function(){

		key = info.key;
		var roomuuid = roomuuid_current;
		console.log(roomuuid);
		LectureService.getPPTpageCount(key, function(resp){

			if(resp.status != "200"){
				$.closeModal();
				$.toast("文档转换失败");
				return;
			}

			$.promptConvertProgressUpdate("5%", "获取幻灯片页数<img style=\"margin-left:5px;  width: 18px;\"  src=\"img/loading.gif\">","");

			flag = false;
			function next(){
    			
    			QiniuService.getPrefopStatus(resp.result, function(resp){

    				if(resp.status !="200"){
    					$.closeModal();
						$.toast("文档转换失败");
						return;
    				}
					var resp_json = JSON.parse(resp.result);

    				if(resp_json.code == 0){

    					var targetcountDoc = resp_json.items[0].key;
    					
    					
    					$.ajaxSettings.async = false; 
						$.getJSON("http://qiniucdn.haojs.com.cn/o_1apa959ns1rnfl851s60nrasrua.pptx_count", function(resp){

							page_count = resp.page_count;
						});
						$.ajaxSettings.async = true; 


						$.promptConvertProgressUpdate("10%", "已获取幻灯片共 "+page_count+" 页","");    					

						//TODO: hxy 开始转化
						LectureService.uploadPPT(key,roomuuid,page_count, function(resp){

							console.log(resp);
							if(resp.status !="200"){
		    					$.closeModal();
								$.toast("文档转换失败");
								return;
		    				}
							var t = 1;	

							//再加一个循环，1个ppt页面1.5秒钟

							$.promptConvertProgressUpdate(parseInt(10+(t/page_count)*90)+"%", "已转化 "+t+"/"+page_count+" 页","");    
							
							function next(){
								
								
								t++;
								
								$.promptConvertProgressUpdate(parseInt(10+(t/page_count)*90)+"%", "已转化 "+t+"/"+page_count+" 页",""); 
								
								

								if(t >= page_count){

									$.closeModal();
									$.alert("恭喜您，已完成PPT上传", function(e){
										$.refreshPage();
									});
									return;
								}

								

								setTimeout(next,1500);
							}
							next();



						});

						flag = true;

    				}else{
    					
    					flag = false;

    				}

    			});

    			if(flag){
    				return;
    			}
	    		setTimeout(next,1000);
	    	}
    		next();

			

		});

	}

	function errorFunc(up, err, errTip){
		$.hidePreloader();
		if(err.code == -600){
			$.toast("上传的图片大小不能超过60MB");
		}else{
			$.toast("您的网络连接不太稳定，请重试");
		}
	}
	return {
		directive_switchtotubeupload:function(){
			$(".tubeupload").click(function(){
				var encry = $.getHashUrlParam("encry");
				$.redirectTo("#/tubeupload?encry="+encry);				
			});

		},
		directive_logout: function(){

			$(".logout").click(function(){

				$.fn.cookie('unionid', "");
				$.redirectTo($.wjtBaseFront + "/pc_login");				
			
			});

		},
		directive_tubeupload: function(){
			$(".tubeupload").click(function(){
				var id = "uploadTubeButton";
				
				var roomuuid = $(this).data("roomuuid");
				var pageurl = "";
				if($(this).data("data-pagetype") == "tube"){
					pageurl = $(this).data("pageurl");
				}
				$.promptTubeUpload(id,pageurl,function(val){
					LectureService.uploadTube(roomuuid,val, function(resp){
						
						if(resp.status == "200"){
							$.refreshPage();
						}else{
							alert("保存失败");
						}
					});
				});
				
			});
			
		},
		directive_pptupload: function(){


			$(".pptupload").click(function(){
				var id = "uploadPPTButton";
				$.promptPptUpload(id);


				roomuuid_current = $(this).data("roomuuid");
				page_count = 0;
				key = "";


				UploadPptUtils.init(id,"haojsqiniu",beforeuploadFunc,uploadedFunc, errorFunc, progressFunc);
			});

		},
		directive_popuproom: function(){

			$(".popuproom").click(function(){

				var roomuuid = $(this).data("roomuuid");
				var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxdd8f1df2a974f04f&redirect_uri=http://www.haojs.com.cn/form/api/wm/route&response_type=code&scope=snsapi_base&state=/wm/room/"+roomuuid+"/attend#wechat_redirect";
    			var imgdiv = "<img src=\"/basic/api/wm/admin/qr/room/"+roomuuid+"\" style=\"width:100%;height:auto\">";
    			$.alert("扫一扫下方二维码进入直播间<br>" + imgdiv);
    			
			});
		

		}

	};

});
